/**
 * @description 群清单下载 API
 * @author 韩
 */

import { API_MECHAN } from "."

const { GETTASKLIST_API, TASKFILE_API, TASKCANCELBYID_API, GETCODETARGET_API, CREATETASK_API } = require("@/Interface/task_api")
const { post } = require("@/utils/request")
const { message } = require("antd")



const createTask = async ({ params, success, failure, complate }) => {
    try {
        const res = await post(CREATETASK_API, {
            ...params,

        })
        success(res)
    } catch (error) {
        message.error("网络连接失败")
    } finally {
        complate()
    }
}

// 获取清单列表
const getTaskList = async ({ params, success, failure }) => {
    try {
        const res = await post(GETTASKLIST_API, {
            ...params,
            userCode: sessionStorage.userCode
        })
        if (res.responseCode === "0000") {
            success(res)
        } else {
            failure(res)
            message.error(res.responseMsg)
        }
    } catch (error) {
        failure(error)
    }
}

// 根据传入的taskId 下载文件
const downTaskFile = async ({ params, success, failure }) => {
    try {
        const res = post(TASKFILE_API, { ...params })
        console.log("下载：", res)
    } catch (error) {

    }
}

// 根据传入的任务id作废待处理任务
const cancelTaskbyId = async ({ params, success, failure }) => {
    try {
        const res = await post(TASKCANCELBYID_API, {
            statTask: {
                jobStatus: "4",
                operateBy: sessionStorage.userCode,
                ...params
            }

        })
        if (res.responseCode === "0000") {
            success(res)
        } else {
            failure(res)
        }
    } catch (error) {
        failure(error)
    }
}

// 查询二级机构/三级机构
const queryUnit = async ({ params, success, failure }) => {
    try {
        const res = await post(API_MECHAN, {
            ...params,
            userCode: sessionStorage.userCode,
        })
        // console.log("二级机构三级机构搜索结果：\n", res)
        if (res.responseCode === "0000") {
            success(res)
        } else {
            message.error(res.responseMsg)
            failure(res)
        }
    } catch (error) {
        failure(error)
    }
}

// 获取任务列表指标配置
const getCodeTargetList = async () => {
    try {
        console.log("获取任务列表指标配置")
        const res = await post(GETCODETARGET_API, {
            userCode: sessionStorage.userCode
        })
        if (res.responseCode) {
            sessionStorage.codeTarget = JSON.stringify(res.data)
        } else {

        }
    } catch (error) {

    }
}

export {
    createTask,
    getCodeTargetList,
    getTaskList,
    downTaskFile,
    cancelTaskbyId,
    queryUnit
}